package com.ebelter.ty.ui.fragment.scale.others;


import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.beans.ScaleUser;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.ty.AppConfig;
import com.ebelter.ty.R;
import com.ebelter.ty.model.http.TyNetUtil;
import com.ebelter.ty.model.http.bean.OriginalParametersResponse;
import com.ebelter.ty.ui.fragment.scale.Scale_M_B5_Fragment;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import static com.ebelter.ty.ui.fragment.scale.others.Scale_M_NAME_Fragment.updateLocalData;


public class Scale_M_HEIGHT_Fragment extends BaseFragment implements View.OnClickListener {


    public static final String TAG = "Scale_M_NAME_Fragment";


    private ImageView modifyheight_back_bt;
    private EditText modify_ed;
    private TextView modifyheight_done_tv;

    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_modifyheight;
    }

    @Override
    protected void initView() {
        super.initView();
        FV();
    }

    @Override
    protected void initData(Bundle arguments) {
        super.initData(arguments);

    }

    private void FV() {
        modifyheight_back_bt = mRootView.findViewById(R.id.modifyheight_back_bt);
        modify_ed = mRootView.findViewById(R.id.et_height_pager);
        modifyheight_done_tv = mRootView.findViewById(R.id.modifyheight_done_tv);

        int h = UserSpUtil.readInt(Constants.IUser.HEIGHT);
        modify_ed.setText(h+"");

        modifyheight_back_bt.setOnClickListener(this);
        modifyheight_done_tv.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.modifyheight_back_bt:
                getActivity().finish();
                break;
            case R.id.modifyheight_done_tv:
                modifyHeight();
                break;

        }
    }

    private void modifyHeight() {
        String height=modify_ed.getText().toString().trim();
        final int h = NumUtils.string2Int(height);
        if (h< AppConfig.HEIGHT_MIN||h>AppConfig.HEIGHT_MAX){
            ToastUtil.show(R.string.Please_enter_a_value_between_20_and_250_height);
            return;
        }
        int originalHeight = UserSpUtil.readInt(Constants.IUser.HEIGHT);
        if (h==originalHeight){
            ToastUtil.show(R.string.Modify_Success);
            getActivity().finish();
            return;
        }
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }
        Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put("userId", UserSpUtil.readLong(Constants.IUser.USER_ID));
        paramMap.put("height",h);
        TyNetUtil.getInstance().updateInfo(this, paramMap, new HttpResponse<OriginalParametersResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, OriginalParametersResponse originalParametersResponse,String s) {
                if (isRequestSuccess){

                    if (originalParametersResponse.resultCode==0){
                        ToastUtil.show(R.string.Set_success);

                        updateLocalData((HashMap<String,Serializable>)originalParametersResponse.originalParameters);
                        CommonEventBus eventBus=new CommonEventBus(TAG, Scale_M_B5_Fragment.TAG,1,null);
                        EventBus.getDefault().post(eventBus);

                        ScaleUser.getUser().setHeight(h);
                        if (getActivity()!=null)getActivity().finish();

                    }else {
                        ToastUtil.show(R.string.Modify_Failed);
                    }

                }else {
                    ToastUtil.show(R.string.Network_connection_error);
                }


            }
        });
    }
}
